package Test.code;

import java.io.File;
import java.io.FileWriter;

import Test.tools.Utils;


public class MapperGener {

	public static void mapperTest(String tableName,String tableBean,String currentBao) throws  Exception{
				String rn = "\r\n";
				String []array = Utils.stringToArray(tableBean);
				StringBuilder sbname = new StringBuilder();
				for (int i = 0; i < array.length ; i++) {
					String bean = 
						array[i]+",";
					sbname.append(bean);
				}
				StringBuilder sbvalue = new StringBuilder();
				for (int i = 0; i < array.length ; i++) {
					String value = "";
					if(i == 0){
						value ="null,";
					}else{
					 value = "#{"+
							array[i]+"},";
				     }
					sbvalue.append(value);
				}
				StringBuilder sbupdateset = new StringBuilder();
				for (int i = 1; i < array.length ; i++) {
					String updateset = "";
					if(i == array.length){
						 updateset = "			<if test=\""+array[i]+" != null \">"+array[i]+" = #{"+array[i]+"}</if>   " + rn ;
					}else{
					     updateset = "			<if test=\""+array[i]+" != null \">"+array[i]+" = #{"+array[i]+"},</if>   " + rn ;
					}
					sbupdateset.append(updateset);
				}
				String src = 
				"<?xml version=\"1.0\" encoding=\"UTF-8\"?>    " + rn +
				"<!DOCTYPE mapper PUBLIC \"-//mybatis.org//DTD Mapper 3.0//EN\" \"http://mybatis.org/dtd/mybatis-3-mapper.dtd\" >    " + rn +
				"<mapper namespace=\""+currentBao+".dao."+Utils.upperFirstChar(tableName)+"Dao\">   " + rn +
				rn +
				"    <select id=\"all"+Utils.upperFirstChar(tableName)+"\" parameterType=\"java.util.HashMap\" resultType=\""+currentBao+".bean."+Utils.upperFirstChar(tableName)+"\">   " + rn +
				"    	select * from "+tableName+" where 1=1 limit #{nowpage} , #{pagesize}   " + rn +
				"    </select>   " + rn +
				
				
				rn +     
				"    <delete id=\"del"+Utils.upperFirstChar(tableName)+"\" parameterType=\"int\">   " + rn +
				"    	delete from "+tableName+" where "+array[0]+" = #{ "+array[0]+"}   " + rn +
				"    </delete>   " + rn +
				    
				"    <insert id=\"add"+Utils.upperFirstChar(tableName)+"\" parameterType=\""+currentBao+".bean."+Utils.upperFirstChar(tableName)+"\">   " + rn +
				"		INSERT INTO "+tableName+"   " + rn +
				"		("+
				sbname.substring(0,sbname.length()-1)+
				")   " + rn +
				"		VALUE ("+
				sbvalue.substring(0,sbvalue.length()-1)+
				")   " + rn +
				"	</insert>   " + rn +
				rn +	
				"	<select id=\""+tableName+"\" resultType=\""+currentBao+".bean."+Utils.upperFirstChar(tableName)+"\">   " + rn +
				"    	select * from "+tableName+" where  "+array[0]+" = #{ "+array[0]+"}   " + rn +
				"    </select>   " + rn +
				rn +	
				"	 <update id=\"update"+Utils.upperFirstChar(tableName)+"\" parameterType=\""+currentBao+".bean."+Utils.upperFirstChar(tableName)+"\" >   " + rn +
				"		update "+tableName  + rn +
				"		 <set>   " + rn +
				sbupdateset +
				"		</set>   " + rn +
				"		where   "+array[0]+" = #{ "+array[0]+"}   " + rn +
				"	</update>	   " + rn +
				rn +	
				
				"	<select id=\"count"+Utils.upperFirstChar(tableName)+"\" resultType=\"Integer\">"  + rn + 
			    "		select count(1) from "+tableName+" where  1=1   " +rn +
			    "	</select>" +rn +
				
				"</mapper>   " + rn ;
				
				String path  = System.getProperty("user.dir")+"/src/"+Utils.docToBackslash(currentBao)+"/mapper/";
				File fpath = new File(path);
				if (!fpath.exists()) {
				   fpath.mkdirs();
			    }
				String fileName = System.getProperty("user.dir")+"/src/"+Utils.docToBackslash(currentBao)+"/mapper/"+Utils.upperFirstChar(tableName)+"Mapper.xml";
				File f = new File(fileName);
				FileWriter fw = new FileWriter(f);
				fw.write(src);
				fw.flush();
				fw.close();
	 }

	public static void mapperTest_(String tableName,String firstTable,String tableBean,String currentBao) throws  Exception{
		String rn = "\r\n";
		String []array = Utils.stringToArray(tableBean);
		StringBuilder sbname = new StringBuilder();
		for (int i = 0; i < array.length ; i++) {
			String bean = 
				array[i]+",";
			sbname.append(bean);
		}
		StringBuilder sbvalue = new StringBuilder();
		for (int i = 0; i < array.length ; i++) {
			String value = "";
			if(i == 0){
				value ="null,";
			}else{
			 value = "#{"+
					array[i]+"},";
		     }
			sbvalue.append(value);
		}
		StringBuilder sbupdateset = new StringBuilder();
		for (int i = 1; i < array.length ; i++) {
			String updateset = "";
			if(i == array.length){
				 updateset = "			<if test=\""+array[i]+" != null \">"+array[i]+" = #{"+array[i]+"}</if>   " + rn ;
			}else{
			     updateset = "			<if test=\""+array[i]+" != null \">"+array[i]+" = #{"+array[i]+"},</if>   " + rn ;
			}
			sbupdateset.append(updateset);
		}
		String src = 
		"<?xml version=\"1.0\" encoding=\"UTF-8\"?>    " + rn +
		"<!DOCTYPE mapper PUBLIC \"-//mybatis.org//DTD Mapper 3.0//EN\" \"http://mybatis.org/dtd/mybatis-3-mapper.dtd\" >    " + rn +
		"<mapper namespace=\""+currentBao+".dao."+Utils.upperFirstChar(tableName)+"Dao\">   " + rn +
		rn +
		"    <select id=\"all"+Utils.upperFirstChar(tableName)+"\" parameterType=\"int\" resultType=\""+currentBao+".bean."+Utils.upperFirstChar(tableName)+"\">   " + rn +
		"    	select * from "+tableName+"  where "+firstTable+"Id =#{ "+firstTable+"Id}  " + rn +
		"    </select>   " + rn +
		rn +     
		"    <delete id=\"del"+Utils.upperFirstChar(tableName)+"\" parameterType=\"int\">   " + rn +
		"    	delete from "+tableName+" where "+array[0]+" = #{ "+array[0]+"}   " + rn +
		"    </delete>   " + rn +
		    
		"    <insert id=\"add"+Utils.upperFirstChar(tableName)+"\" parameterType=\""+currentBao+".bean."+Utils.upperFirstChar(tableName)+"\">   " + rn +
		"		INSERT INTO "+tableName+"   " + rn +
		"		("+
		sbname.substring(0,sbname.length()-1)+
		")   " + rn +
		"		VALUE ("+
		sbvalue.substring(0,sbvalue.length()-1)+
		")   " + rn +
		"	</insert>   " + rn +
		rn +	
		"	<select id=\""+tableName+"\" resultType=\""+currentBao+".bean."+Utils.upperFirstChar(tableName)+"\">   " + rn +
		"    	select * from "+tableName+" where  "+array[0]+" = #{ "+array[0]+"}   " + rn +
		"    </select>   " + rn +
		rn +	
		"	 <update id=\"update"+Utils.upperFirstChar(tableName)+"\" parameterType=\""+currentBao+".bean."+Utils.upperFirstChar(tableName)+"\" >   " + rn +
		"		update "+tableName  + rn +
		"		 <set>   " + rn +
		sbupdateset +
		"		</set>   " + rn +
		"		where   "+array[0]+" = #{ "+array[0]+"}   " + rn +
		"	</update>	   " + rn +
		rn +	
		"</mapper>   " + rn ;
		
		String path  = System.getProperty("user.dir")+"/src/"+Utils.docToBackslash(currentBao)+"/mapper/";
		File fpath = new File(path);
		if (!fpath.exists()) {
		   fpath.mkdirs();
	    }
		String fileName = System.getProperty("user.dir")+"/src/"+Utils.docToBackslash(currentBao)+"/mapper/"+Utils.upperFirstChar(tableName)+"Mapper.xml";
		File f = new File(fileName);
		FileWriter fw = new FileWriter(f);
		fw.write(src);
		fw.flush();
		fw.close();
}



	
}
